package com.sniff.atp.service.login;

import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.dto.DKLoginDTO;
import com.sniff.atp.enums.api.CustomerAPI;
import com.sniff.atp.utils.DBUtil;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.login
 * @Author: zxy
 * @CreateTime: 2022-10-31 10:57
 * @Description: 档口小程序登录
 * @Version: 1.0
 */
@Service
public class DKLoginService {
    /**
     * 小程序：登录
     * @param email
     * @param password
     * @return
     */
    public DKLoginDTO login(String email, String password){
        JSONObject param = new JSONObject();
        param.put("email", email);
        param.put("password", password);

        String result = new HttpClientUtil().doPostJson(CustomerAPI.DK_LOGIN.getApi(), param);
        DKLoginDTO dkLoginDTO = new DKLoginDTO();
        dkLoginDTO.setToken(JsonUtil.extractString(result, "data"));
        return dkLoginDTO;
    }

    /**
     * 小程序：根据档口市场选择对应的小程序账号
     * @param stallName
     * @return
     */
    public DKLoginDTO login(String stallName){

        JSONObject object = DBUtil.selectOne("Purchase-查询档口市场账户", new ContentDTO("1", stallName));

        String email = object.getString("email");
        return login(email, "123456");
    }
}
